<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/3/21
 * Time: 17:49
 */

namespace app\model\user;


use app\common\lib\Common;
use app\common\lib\Constant;
use app\model\BaseModel;

class UserRechargeOrderModel extends BaseModel
{
    protected $table = 'video_user_recharge_order';

    protected $primaryKey = 'id';

    protected static $instance;

    public $timestamps = false;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function saveData($param)
    {
        if (!empty($param['order_no']) || !empty($param['trade_no'])) {
            $find = false;
            !empty($param['order_no']) && $find = $this->where(['order_no' => $param['order_no']])->first();
            $find==false && $find = $this->where(['trade_no' => $param['trade_no']])->first();

            if($find==false){
                throw new UserException(4020,'订单不存在');
            }
            $update = [];
            !empty($param['user_id']) && $update['user_id'] = $param['user_id'];
            !empty($param['product_type']) && $update['product_type'] = $param['product_type'];
            !empty($param['trade_no']) && $update['trade_no'] = $param['trade_no'];
            !empty($param['original_trade_no']) && $update['original_trade_no'] = $param['original_trade_no'];
            !empty($param['order_status']) && $update['order_status'] = $param['order_status'];
            !empty($param['pay_status']) && $update['pay_status'] = $param['pay_status'];
            !empty($param['pay_time']) && $update['pay_time'] = $param['pay_time'];
            !empty($param['money']) && $update['money'] = $param['money'];
            !empty($param['fail_reason']) && $update['fail_reason'] = $param['fail_reason'];
            !empty($param['payment']) && $update['payment'] = $param['payment'];
            //!empty($param['bank_account']) && $update['bank_account'] = $param['bank_account'];
            //!empty($param['first_validate_time']) && $update['first_validate_time'] = $param['first_validate_time'];
            //!empty($param['bank_code']) && $update['bank_code'] = $param['bank_code'];
            !empty($param['product_sn']) && $update['product_sn'] = $param['product_sn'];
            !empty($param['expires_date']) && $update['expires_date'] = $param['expires_date'];
            !empty($param['update_pay_status_source']) && $update['update_pay_status_source'] = $param['update_pay_status_source'];


            isset($param['isrenew']) && $update['isrenew'] = $param['isrenew'];
            !empty($param['notify_time']) && $update['notify_time'] = $param['notify_time'];
            !empty($param['notification_type']) && $update['notification_type'] = $param['notification_type'];
            !empty($param['subtype']) && $update['subtype'] = $param['subtype'];
            isset($param['is_trial']) && $update['is_trial'] = $param['is_trial'];
            isset($param['is_test']) && $update['is_test'] = $param['is_test'];

            $result = $this->getWhere(['orderid'=>$find['orderid']])->update($update);
            if($result==false){
                throw new UserException(4020,'更新订单失败');
            }
            return $result;

        } else {
            return $this->insertData($param);
        }
    }

    public function insertData($param)
    {
        if(isset($param[0])){
            //批量添加
            $maxId =  $this->where([['order_no','>',0]])->max('order_no');
            $orderOn = Common::getSnCode($maxId,'11',8, 2);

            $insert = [];
            foreach($param as $val){
                $insert[] = [
                    'user_sn'=> isset($val['user_sn']) ? $val['user_sn']: (request()->curUserId??'0'),
                    'order_no'=>$orderOn,
                    'product_type'=>$val['product_type'],
                    'trade_no'=>$val['trade_no'],
                    'original_trade_no'=>$val['original_trade_no'],
                    'order_status'=>$val['order_status']??Constant::ORDER_STATUS_APY_WAIT,
                    'pay_status'=>$val['pay_status']??Constant::ORDER_PAY_STATUS_WAIT,
                    'money'=>$val['money'],
                    'pay_time'=>$val['pay_time'],
                    'payment'=>$val['payment'],
                    'device_number'=>request()->deviceNumber ?? '',
                    'ip'=>Common::getReadIp(),

                    'isrenew'=>$val['isrenew'] ?? 0,
                    'product_id'=>$val['product_id'] ?? '',
                    'expires_date'=>$val['expires_date'] ?? 0,
                    'is_trial'=>$val['is_trial'] ?? 0,
                ];

                $orderOn = Common::getSnCode($orderOn,'11',8, 2);
            }
            $insert && $this->insert($insert);
            return true;
        }else{
            $maxId =  $this->where([['order_no','>',0]])->max('order_no');
            $orderOn = Common::getSnCode($maxId,'11',8, 2);

            $data = [];
            $data['user_sn'] = isset($param['user_sn']) ? $param['user_sn'] : (request()->curUserId??'');
            $data['order_no'] = $orderOn;
            $data['product_type'] = $param['product_type'];
            $data['trade_no'] = $param['trade_no']??'';
            $data['original_trade_no'] = $param['original_trade_no']??'';
            $data['order_status'] = $param['order_status'];
            $data['pay_status'] = $param['pay_status'];
            $data['payment'] = $param['payment']??1;

            $data['money'] = $param['sale_price'] ? $param['sale_price'] : $param['price'];
            $data['isrenew'] = $param['isrenew']??0;
            $data['product_id'] = $param['product_id'];
            $data['expires_date'] = $param['expires_date']??0;
            $data['update_pay_status_source'] = $param['update_pay_status_source']??1;

            $data['ip'] =   request()->getRealIp();
            $data['device_number'] = request()->deviceNumber ?? '';

            $id = $this->insertGetId($data);

            return $orderOn;
        }
    }
}